﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Security.Cryptography;
using JLPT.Models.Objects;
using JLPT.Models;

namespace JLPT.Common
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString FormatSub(this HtmlHelper htmlHelper, int number, QuestionSub sub, bool selected = false)
        {
            string strReturn = string.Empty;

            switch (number)
            {
                case 1:
                    strReturn = "A";
                    break;
                case 2:
                    strReturn = "B";
                    break;
                case 3:
                    strReturn = "C";
                    break;
                case 4:
                    strReturn = "D";
                    break;
                case 5:
                    strReturn = "E";
                    break;
                case 6:
                    strReturn = "F";
                    break;
                case 7:
                    strReturn = "G";
                    break;
                case 8:
                    strReturn = "H";
                    break;
                default:
                    break;
            }

            string strSelect = string.Empty;
            if (selected)
            {
                strSelect = "color:#FF0000";
            }

            if (string.IsNullOrEmpty(sub.Name))
            {
                strReturn = "";
            }
            else {
                strReturn = string.Format("<span style='margin:5px 0 10px 30px;{2}'>{0}.{1}</span>", strReturn, sub.Name, strSelect);
            }
            return MvcHtmlString.Create(strReturn);
        }

        public static MvcHtmlString WrongAnswer(this HtmlHelper htmlHelper, bool isWrong)
        {
            string strReturn = string.Empty;

            if (isWrong)
            {
                strReturn = "background:#FF0000;text-decoration: line-through";
            }

            return MvcHtmlString.Create(strReturn);
        }

        public static MvcHtmlString WrongAnswer(this HtmlHelper htmlHelper, QuestionDetail detail, string selecter)
        {
            string strReturn = string.Empty;
            string strImg = "<img src='/Content/Images/right.png' />";
          
            //get detail name
            strReturn = detail.Answer;

            if (!detail.IsRight & selecter.Equals(detail.Id.ToString()))
            {
                strReturn = string.Format("<span style='background:#FF0000;text-decoration: line-through'>{0}</span>", strReturn);
            }

            if (detail.IsRight)
            {
                strReturn += strImg;
            }

            return MvcHtmlString.Create(strReturn);
        }

        public static MvcHtmlString QuestionNameTest(this HtmlHelper htmlHelper, int sequence, Question question, bool isResultPage) {
            return QuestionName(sequence, question, false);
        }

        public static MvcHtmlString QuestionNameResult(this HtmlHelper htmlHelper, int sequence, Question question, bool isResultPage)
        {
            return QuestionName(sequence, question, true);
        }

        private static IList<QuizHistory> HistoryStorage;
        private static MvcHtmlString QuestionName(int sequence ,Question question, bool isResultPage)
        {
            bool isFavorite = true;
            string strReturn = string.Empty;

            //History
            string favourite = string.Empty;
            if (HistoryStorage == null)
            {
                JLPTContext db = new JLPTContext();
                HistoryStorage = db.QuizHistories.ToList();
            }

            QuizHistory history = HistoryStorage.FirstOrDefault(x => x.QuestionId.Equals(question.Id));

            if (history == null || !history.Favourite)
            {
                isFavorite = false;
            }
            else {
                favourite = "<img id='imgFavorite' src='/Content/Images/favorite.png'/>";
            }
           

            string strCursor = string.Empty;
            if (isResultPage)
            {
                strCursor = "cursor:pointer";
            }
            strReturn = string.Format("<div style='margin:10px 0 0 0;{4}' id='Question-{0}' favorite ='{5}'>{1}. {2} {3}</div>",
                                    question.Id, sequence, question.Name, favourite, strCursor, isFavorite);

            foreach (QuestionSub item in question.QuestionSubs)
            {
                if (!string.IsNullOrEmpty(item.Name))
                {
                    strReturn = strReturn.Replace(item.Name.Trim(),
                        string.Format("<span style='border:#FF0000 1px solid;background:#F3F781'>{0}</span>",item.Name.Trim()));     
                }
            }

            return MvcHtmlString.Create(strReturn);
        }

       
       
    }
}